//
//  FavFactoryViewController.m
//  Rail
//
//  Created by hychen on 2018/2/27.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "FavFactoryViewController.h"
#import "WorkAreaFavTableViewCell.h"
#import "FactoryDetailViewController.h"

@interface FavFactoryViewController ()

@end

@implementation FavFactoryViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)refreshData {
    BMWeakSelf
    [HttpRequest loadWorkAreaFavDataListSuccess:^(NSArray *object) {
        weakSelf.dataArray = [NSMutableArray arrayWithArray:object?:@[]];
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_header endRefreshing];
    } failure:^(NSString *code, NSString *msg) {
        [weakSelf.tableView.mj_header endRefreshing];
        if ([code isEqualToString:@"309"]) {
            weakSelf.dataArray = [NSMutableArray arrayWithArray:@[]];
        }
        else {
            Show_Msg_Error(msg)
        }
        [weakSelf.tableView reloadData];
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50.f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WorkAreaFavModel *model = [self.dataArray objectAtIndex:indexPath.row];
    WorkAreaFavTableViewCell *cell = [WorkAreaFavTableViewCell cellWithTableView:tableView class:[WorkAreaFavTableViewCell class]];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WorkAreaFavModel *model = [self.dataArray objectAtIndex:indexPath.row];
    //工区详情
    FactoryDetailViewController *vc = [FactoryDetailViewController new];
    vc.areaid = model.areaid;
    vc.navTitle = model.area_name;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - tableView自带的编辑功能
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
    }
}
// 选择编辑的样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}
// 修改delete的文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}

// 设置显示多个按钮
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BMWeakSelf
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        WorkAreaFavModel *model = [self.dataArray objectAtIndex:indexPath.row];
        //删除
        [weakSelf deleteRowWithFoldModel:model indexPath:indexPath];
    }];
    deleteRowAction.backgroundColor = [UIColor redColor];
    
    return @[deleteRowAction];
}

#pragma mark - 操作
//删除Row
- (void)deleteRowWithFoldModel:(WorkAreaFavModel *)model indexPath:(NSIndexPath *)indexPath {
    [self cancelFavWithModel:model indexPath:indexPath];
}

//取消收藏
- (void)cancelFavWithModel:(WorkAreaFavModel *)model indexPath:(NSIndexPath *)indexPath {
    Show_ActionEnabled
    Show_NoMsg
    BMWeakSelf
    [HttpRequest cancelFavWithFavId:model.favid success:^(id object) {
        [weakSelf.dataArray removeObjectAtIndex:indexPath.row];
        [weakSelf.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        Dismiss_NoMsg
        Show_ActionDisabled
    } failure:^(NSString *code, NSString *msg) {
        Show_Msg_Error(msg)
        Show_ActionDisabled
    }];
}
@end
